package org.ht.demo.openapi.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.ht.demo.openapi.bean.IotZCdgLog;
import org.ht.demo.openapi.bean.MapBean;

import java.math.BigInteger;
import java.util.*;

public class CodeuTIL {
    public static ByteBuf getbackdata(MapBean bmap) {
        String bdata=bmap.get("PVER").toString()+bmap.get("CMD").toString()+bmap.get("CMDVER").toString()+"00"+ StringUtil.padLeft(Integer.toHexString(MdUtils.getconfigdata().length()/2)+"",4)+bmap.get("CMDNo")+Long.toHexString(System.currentTimeMillis()/1000L)+"FFFFFF80"+MdUtils.getconfigdata();
        bdata=bdata+StringUtil.padLeft(CRCUtils.getCRCString(bdata),8);
        ByteBuf bufff = Unpooled.buffer();// netty需要用ByteBuf传输
        bufff.writeBytes(StringUtil.hexString2Bytes(bdata));// 对接需要16进制
        return bufff;
    }
    public static MapBean buileMdUdp(String data) {
        //if(data.substring(data.length()-8, data.length()).equalsIgnoreCase(StringUtil.padLeft(CRCUtils.getCRCString(data.substring(0, data.length()-8)),8))){//CRC校验
        if(true){//CRC校验

            MapBean mapBean=new MapBean();
            mapBean.put("PVER", DeviceUtils.buileWei(data,1,0));
            mapBean.put("CMD", DeviceUtils.buileWei(data,2,0));
            mapBean.put("CMDVER", DeviceUtils.buileWei(data,3,0));
            mapBean.put("CMDVER2", DeviceUtils.buileWei(data,4,0));
            mapBean.put("Len", Integer.parseInt(DeviceUtils.buileWei(data,5,6),16));
            mapBean.put("CMDNo", DeviceUtils.buileWei(data,7,8));
            mapBean.put("MAC", DeviceUtils.buileWei(data,9,14).toLowerCase());
            mapBean.put("HOSTID", DeviceUtils.buileWei(data,16,0));  //电箱编号
            //
            mapBean.put("dntype",3);//网关设备-空开

//            mapBean.put("DATA", DeviceUtils.buileWei(data,17,Integer.parseInt(mapBean.get("Len")+"")+16));
            String rdata=DeviceUtils.buileWei(data,17,Integer.parseInt(mapBean.get("Len")+"")+16);   //总数据长度 节点1-3
            if("b0".equalsIgnoreCase(mapBean.get("CMD").toString())){  //注册


            }else if("b2".equalsIgnoreCase(mapBean.get("CMD").toString())){  //网络配置消息
                mapBean.put("NNO", DeviceUtils.buileWei(rdata,1,0));
                mapBean.put("NTYPE", DeviceUtils.buileWei(rdata,2,0));
                mapBean.put("MODEL", DeviceUtils.buileWei(rdata,3,4));
                mapBean.put("VER", DeviceUtils.buileWei(rdata,5,6));
                mapBean.put("CTRL", DeviceUtils.buileWei(rdata,13,14));
                mapBean.put("RTVI", DeviceUtils.buileWei(rdata,37,38));
                mapBean.put("IMEI","0");
                mapBean.put("IMSI","0");
                if("ed".equalsIgnoreCase(mapBean.get("NTYPE").toString())||"8B".equalsIgnoreCase(mapBean.get("NTYPE").toString())||"8C".equalsIgnoreCase(mapBean.get("NTYPE").toString())||"8D".equalsIgnoreCase(mapBean.get("NTYPE").toString())||"8E".equalsIgnoreCase(mapBean.get("NTYPE").toString())){
                    mapBean.put("IMEI", Integer.parseInt(DeviceUtils.buileWei(rdata,85,88),16)+ ""+Integer.parseInt( DeviceUtils.buileWei(rdata,89,92),16));
                    mapBean.put("IMSI",  Integer.parseInt(DeviceUtils.buileWei(rdata,93,96),16)+ ""+Integer.parseInt(DeviceUtils.buileWei(rdata,97,100),16));
                }
            }else if("b4".equalsIgnoreCase(mapBean.get("CMD").toString())) {  //配置消息

                List<Map<String,String>> configList=new ArrayList<>();
                for(int i=1;i<=Integer.parseInt(mapBean.get("CMDVER").toString());i++) {
                    String amdata = rdata.substring(160 * (i - 1), 160 * i); //根据节点数
                    Map<String,String> configMap=new HashMap<>();
                    configMap.put("NNO", DeviceUtils.buileWei(amdata,1,0));
                    configMap.put("DTYPE", DeviceUtils.buileWei(amdata,2,0));  //产品类型
                    configMap.put("CUNIT", Integer.parseInt(DeviceUtils.buileWei(amdata,12,0),16)+"");  //变比倍数
                    configMap.put("DCTRL", DeviceUtils.buileWei(amdata,13,14));  //控制位
                    configMap.put("VOLH", Integer.parseInt(DeviceUtils.buileWei(amdata,15,16),16)+"");  //电压上限
                    configMap.put("VOLL", Integer.parseInt(DeviceUtils.buileWei(amdata,17,18),16)+"");  //电压下限
                    configMap.put("LKIH", DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata,21,22),16,10));  //漏电流上限
                    configMap.put("PWRH", DeviceUtils.buileWei(amdata,25,26));  //功率上限
                    configMap.put("PWRL", DeviceUtils.buileWei(amdata,27,28));  //功率下限
                    configMap.put("TMPH", DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata,31,32),16,10));  //温度上限
                    configMap.put("CURH", DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata,35,36),16,10));  //电流上限
                    configList.add(configMap);
                }
                mapBean.put("configList",configList);

            }else if("ac".equalsIgnoreCase(mapBean.get("CMD").toString())||"ad".equalsIgnoreCase(mapBean.get("CMD").toString())) {  //实时消息
                rdata=rdata.substring(4*2,rdata.length()); //去掉时间戳
                List<IotZCdgLog> zCdgLogList=new ArrayList<>();
                //rdata=rdata.substring(64,rdata.length()); //根据节点数

                //获取有几路
                int luyou=1;
                for(int i=1;i<=Integer.parseInt(mapBean.get("CMDVER").toString());i++){
                    String amdata=rdata.substring(64*(i-1),64*i);
                    String nno=DeviceUtils.buileWei(amdata,1,0);
                    luyou=Integer.parseInt(nno);
                }
                //节点0为网络数据，该数据不解析
                for(int j=1;j<=luyou;j++) {
                    IotZCdgLog iotZCdgLog = new IotZCdgLog();
                    iotZCdgLog.setLinetype("三相");
                    iotZCdgLog.setCommon(data);
                    iotZCdgLog.setSbtype("00");////实时消息
                    iotZCdgLog.setMac(mapBean.get("MAC").toString() + "-0" + j);
                    iotZCdgLog.setNno(j);
                    for (int i = 1; i <= Integer.parseInt(mapBean.get("CMDVER").toString()); i++) {
                        String amdata = rdata.substring(64 * (i - 1), 64 * i); //根据节点数
                        String nno = DeviceUtils.buileWei(amdata, 1, 0);
                        if(j==Integer.parseInt(nno)){
                            iotZCdgLog.setDptype(DeviceUtils.buileWei(amdata, 2, 0));
                            String ALM = DeviceUtils.buileWei(amdata, 21, 24);
                            iotZCdgLog.setKks(bbj(ALM, "kks"));

                            if ("e0".equalsIgnoreCase(iotZCdgLog.getDptype()) || "80".equalsIgnoreCase(iotZCdgLog.getDptype())) {//单项
                                iotZCdgLog.setLinetype("单相");
                                iotZCdgLog.setUa(Integer.parseInt(DeviceUtils.buileWei(amdata, 3, 4), 16) + "");
                                iotZCdgLog.setLeakv(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 5, 6), 16, 10));
                                iotZCdgLog.setPa(Float.parseFloat(Integer.parseInt(DeviceUtils.buileWei(amdata, 7, 10), 16) + "") / 1000 + "");
                                iotZCdgLog.setP(Float.parseFloat(Integer.parseInt(DeviceUtils.buileWei(amdata, 7, 10), 16) + "") / 1000 + "");
                                iotZCdgLog.setTa(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 11, 12), 16, 10));
                                iotZCdgLog.setCa(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 13, 16), 16, 100));

                                Random r = new Random();
                                iotZCdgLog.setTn((Float.parseFloat(iotZCdgLog.getTa()) - Float.parseFloat(r.nextInt(10) + "") / 10) + ""); //处理

                                iotZCdgLog.setTas(bbj(ALM, "ts"));
                                iotZCdgLog.setCas(bbj(ALM, "cs"));
                                iotZCdgLog.setUas(bbj(DeviceUtils.buileWei(amdata, 19, 22), "us"));
                                iotZCdgLog.setTds(bbj(ALM, "ts"));
                                iotZCdgLog.setLeakvs(bbj(ALM, "ls"));

                            }

                            if ("e4".equalsIgnoreCase(iotZCdgLog.getDptype()) || "84".equalsIgnoreCase(iotZCdgLog.getDptype())) {
                                iotZCdgLog.setLeakv(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 5, 6), 16, 10));
                                iotZCdgLog.setP(Float.parseFloat(Integer.parseInt(DeviceUtils.buileWei(amdata, 7, 10), 16) + "") / 1000 + "");
                                iotZCdgLog.setTd(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 11, 12), 16, 10));
                                iotZCdgLog.setTds(bbj(ALM, "ts"));
                                iotZCdgLog.setLeakvs(bbj(ALM, "ls"));

                            }

                            if ("e5".equalsIgnoreCase(iotZCdgLog.getDptype()) || "85".equalsIgnoreCase(iotZCdgLog.getDptype())) {  //A 相
                                iotZCdgLog.setUa(Integer.parseInt(DeviceUtils.buileWei(amdata, 3, 4), 16) + "");
                                iotZCdgLog.setPa(Float.parseFloat(Integer.parseInt(DeviceUtils.buileWei(amdata, 7, 10), 16) + "") / 1000 + "");
                                //iotZCdgLog.setP(Integer.parseInt(DeviceUtils.buileWei(rdata,7,10),16)+"");
                                iotZCdgLog.setTa(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 11, 12), 16, 10));
                                iotZCdgLog.setCa(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 13, 16), 16, 100));
                                iotZCdgLog.setTas(bbj(ALM, "ts"));
                                iotZCdgLog.setCas(bbj(ALM, "cs"));
                                iotZCdgLog.setUas(bbj(DeviceUtils.buileWei(amdata, 19, 22), "us"));
                            }

                            if ("e6".equalsIgnoreCase(iotZCdgLog.getDptype()) || "86".equalsIgnoreCase(iotZCdgLog.getDptype())) {  //B 相
                                iotZCdgLog.setUb(Integer.parseInt(DeviceUtils.buileWei(amdata, 3, 4), 16) + "");
                                iotZCdgLog.setPb(Float.parseFloat(Integer.parseInt(DeviceUtils.buileWei(amdata, 7, 10), 16) + "") / 1000 + "");
                                iotZCdgLog.setTb(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 11, 12), 16, 10));
                                iotZCdgLog.setCb(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 13, 16), 16, 100));

                                iotZCdgLog.setTbs(bbj(ALM, "ts"));
                                iotZCdgLog.setCbs(bbj(ALM, "cs"));
                                iotZCdgLog.setUbs(bbj(ALM, "us"));
                            }
                            if ("e7".equalsIgnoreCase(iotZCdgLog.getDptype()) || "86".equalsIgnoreCase(iotZCdgLog.getDptype())) {  //C 相
                                iotZCdgLog.setUc(Integer.parseInt(DeviceUtils.buileWei(amdata, 3, 4), 16) + "");
                                iotZCdgLog.setPc(Float.parseFloat(Integer.parseInt(DeviceUtils.buileWei(amdata, 7, 10), 16) + "") / 1000 + "");
                                iotZCdgLog.setTc(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 11, 12), 16, 10));
                                iotZCdgLog.setCc(DeviceUtils.ZCdatafilter1(DeviceUtils.buileWei(amdata, 13, 16), 16, 100));
                                iotZCdgLog.setTcs(bbj(ALM, "ts"));
                                iotZCdgLog.setCcs(bbj(ALM, "cs"));
                                iotZCdgLog.setUcs(bbj(ALM, "us"));
                            }
                            //iotZCdgLog.setPf(getAvgPf(iotZCdgLog));
                        }
                    }
                    zCdgLogList.add(iotZCdgLog);
                }
                mapBean.put("iotZCdgLog",zCdgLogList);
            }else if("a4".equalsIgnoreCase(mapBean.get("CMD").toString())) {   //事件消息
                mapBean.put("EVT_LV", 0);
                List<Map<String,String>> alarmList=new ArrayList<>();
                for(int i=1;i<=Integer.parseInt(mapBean.get("CMDVER2").toString());i++) {
                    Map<String,String> alarmMap=new HashMap<>();
                    String amdata=rdata.substring(56*(i-1),56*i); //根据节点数
                    PushEventParseModel.DataModel dataModel = CommonDataParser.toBean(amdata, PushEventParseModel.DataModel.class);
                    alarmMap.put("NNO",dataModel.getEVT_EX());
                    if(Integer.parseInt(dataModel.getEVT_LV())==2){  //报警
                        if("3A".equalsIgnoreCase(dataModel.getEVT_ID())||"35".equalsIgnoreCase(dataModel.getEVT_ID())||"36".equalsIgnoreCase(dataModel.getEVT_ID())||"37".equalsIgnoreCase(dataModel.getEVT_ID())){//超限值报警
                            alarmMap.put("bjt","kk");
                            Integer bjtypr=Integer.parseInt(dataModel.getEVT_RE().substring(1,2),16);
                            alarmMap.put("bjconent",Damtype.getDamtype(Integer.parseInt(dataModel.getEVT_EX().substring(0,1)))+""+ Damtype.Damdress.getDamdress(Integer.parseInt(dataModel.getEVT_EX().substring(1,2),16)));

                            String bjconentValue="";
                            String thresholdValue="";

                            if(bjtypr==4||bjtypr==22) { //温度
                                bjconentValue = DeviceUtils.ZCdatafilter1(dataModel.getALM_N() + "", 16, 10) + "℃";
                                thresholdValue = DeviceUtils.ZCdatafilter1(dataModel.getALM_O() + "", 16, 10) + "℃";
                            }
                            if(bjtypr==6||bjtypr==16) {  //电流
                                bjconentValue = DeviceUtils.ZCdatafilter1(dataModel.getALM_N() + "", 16, 100) + "A";
                                thresholdValue = DeviceUtils.ZCdatafilter1(dataModel.getALM_O() + "", 16, 100) + "A";
                            }
                            if(bjtypr==5||bjtypr==15) {  //漏电
                                bjconentValue = DeviceUtils.ZCdatafilter1(dataModel.getALM_N() + "", 16, 10) + "mA";
                                thresholdValue = DeviceUtils.ZCdatafilter1(dataModel.getALM_O() + "", 16, 10) + "mA";
                            }
                            if(bjtypr==3) {  //过载
                                bjconentValue = Integer.parseInt(dataModel.getALM_N() + "", 16) + "W";
                                thresholdValue = Integer.parseInt(dataModel.getALM_O() + "", 16) + "W";
                            }

                            if(bjtypr==7||bjtypr==12||bjtypr==13||bjtypr==14) {  //过载
                                bjconentValue = Integer.parseInt(dataModel.getALM_N() + "", 16) + "V";
                                thresholdValue = Integer.parseInt(dataModel.getALM_O() + "", 16) + "V";
                            }
                            alarmMap.put("thresholdValue",thresholdValue);
                            alarmMap.put("bjconentValue",bjconentValue);
                            alarmMap.put("bjcode",bjtypr+"");

                        }else if("34".equalsIgnoreCase(dataModel.getEVT_ID())){  //报警改变
                            alarmMap.put("bjt","kk");
                            BigInteger val1 = new BigInteger(dataModel.getALM_N()+"",16);
                            BigInteger val2 = new BigInteger(dataModel.getALM_O()+"",16);
                            alarmMap.put("bjcode",checkbbj(val1.xor(val2).toString(2)).get("bjcode")+"");
                            alarmMap.put("bjconent",checkbbj(val1.xor(val2).toString(2)).get("bjconent")+"");
                        }else if("81".equalsIgnoreCase(dataModel.getEVT_ID())){  //操作事件
                            alarmMap.put("bjt","cz");

                        }else {  //其他电箱事件
                            alarmMap.put("bjt","dx");
//                            log.warn("其他电箱事件:"+data);
                        }
                    }

                    alarmMap.put("EVT_LV", dataModel.getEVT_LV());
                    alarmList.add(alarmMap);

                }
                mapBean.put("alarmList",alarmList);
            }else if("d3".equalsIgnoreCase(mapBean.get("CMD").toString())){  //指令下发回馈消息
                if("00".equalsIgnoreCase(mapBean.get("CMDVER2").toString())){
                    //log.warn("指令下发成功："+mapBean.toString());
                }

            } else{


            }

            return mapBean;
        }
        return null;
    }

    /**
     * 报警定义
     * @return
     */
    public static MapBean checkbbj(String bdata) {
        MapBean mapBean=new MapBean();
        mapBean.put("bjconent","");
        mapBean.put("bjcode","-1");

        String nc=StringUtil.padLeft32(StringUtil.binary(StringUtil.parseHexStr2Byte(bdata),2),32);
        String bjcode="";
        for(int i=nc.length()-1;i >=0;i--) {
            bjcode=bjcode+nc.charAt(i);
        }
        for(int i=0;i < bjcode.length();i++) {
            if(Integer.parseInt(bjcode.charAt(i)+"")==1){
                switch(i+1){
                    case 1 :
                        break;
                    case 2 :
                        break;
                    case 3 :
                        break;
                    case 4 :
                        break;
                    case 5 :
                        break;
                    case 6 :
                        break;
                    case 7 :
                        break;
                    case 8 :
                        break;
                    case 9 :
                        break;
                    case 10:
                        break;
                    case 11 :
                        break;
                    case 12 :
                        break;
                    case 13 :
                        break;
                    case 14 :
                        break;
                    case 16 :
                        break;
                    case 17:
                        break;
                    case 18:
                        break;
                    case 19 :
                        break;
                    case 20 :
                        break;
                    case 22 :
                        break;
                    case 23 :
                        break;
                    case 29 :
                        break;
                    case 30:
                        break;
                    case 32:
                        mapBean.put("bjcode",mapBean.get("bjcode")+"开关在线状态");
                        break;

                    default : //可选
                }
            }

            if(i==30){
                if(Integer.parseInt(bjcode.charAt(i)+"")==1){
                    mapBean.put("bjcode",mapBean.get("bjcode")+"合闸");
                    mapBean.put("bjcode",mapBean.get("bjcode")+","+i+3);
                }else {
                    mapBean.put("bjcode",mapBean.get("bjcode")+"分闸");
                    mapBean.put("bjcode",mapBean.get("bjcode")+","+i+1);
                }
            }

        }

        return mapBean;
    }

    /**
     * 报警定义
     * @return
     */
    public static int bbj(String bdata,String stu) {
        String nc=StringUtil.padLeft32(StringUtil.binary(StringUtil.parseHexStr2Byte(bdata),2),32);
        for(int i=0;i < nc.length();i++) {

            if("kks".equalsIgnoreCase(stu)){  //开闸
                if(i==1){
                    if(Integer.parseInt(nc.charAt(i)+"")==1){
                        return 1;
                    }
                }
                if(i==1){  //合闸
                    if(Integer.parseInt(nc.charAt(i)+"")==0){
                        return 0;
                    }
                }
            }

            if("us".equalsIgnoreCase(stu)){  //电压
                if(i==20){
                    if(Integer.parseInt(nc.charAt(i)+"")==1){
                        return 2;
                    }
                }
                if(i==25){  //过压报警
                    if(Integer.parseInt(nc.charAt(i)+"")==1){
                        return 1;
                    }
                }
            }

            if("cs".equalsIgnoreCase(stu)){  //电流
                if(i==26){  //过流报警
                    if(Integer.parseInt(nc.charAt(i)+"")==1){
                        return 1;
                    }
                }
            }

            if("ls".equalsIgnoreCase(stu)){  //漏电
                if(i==27){
                    if(Integer.parseInt(nc.charAt(i)+"")==1){
                        return 1;
                    }
                }
            }

            if("ts".equalsIgnoreCase(stu)){  //温度
                if(i==28){
                    if(Integer.parseInt(nc.charAt(i)+"")==1){
                        return 1;
                    }
                }
            }

        }
        return 0;
    }
}
